package com.atguigu.srb.core.controller.admin;


import com.atguigu.PageEntity;
import com.atguigu.Result;
import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author ccy
 * @since 2025-04-09
 */
@Api(tags = "积分等级管理")
@CrossOrigin//前后端联调，防止跨域
@RestController
@RequestMapping("/admin/core/integralGrade")
@Slf4j
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    /**
     * 分页查询积分等级
     * @param pageEntity
     * @return IPage<IntegralGrade>
     */
    @ApiOperation("分页查询积分等级")
    @PostMapping("/query")
    public Result<IPage<IntegralGrade>> selectByPage(@RequestBody PageEntity<IntegralGrade> pageEntity) {
       IPage<IntegralGrade> page = this.integralGradeService.selectByPage(pageEntity);

        return Result.setSucData(page);
    }


    /**
     * 批量删除积分等级
     * @param ids
     * @return Result<?>
     */
    @ApiOperation("批量删除积分等级")
    @PostMapping("/batchDelete")
    public Result<?> deleteByIds(@RequestBody List<String> ids) {

        return this.integralGradeService.deleteByIds(ids);
    }

    /**
     * 获取所有数据
     * @return R
     */
    @ApiOperation("获取所有数据")
    @PostMapping("/list")
    public R listAll() {

        log.info("hi this is a log info");
        log.warn("hi this is a log warn");
        log.error("hi this is a log error");

        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list", list).message("查询成功");
    }

    /**
     * 新增积分等级
     * @param integralGrade
     * @return R
     */
    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R save(@RequestBody IntegralGrade integralGrade) {

        //自定义异常
        /*if (StringUtils.isNotNull(integralGrade.getBorrowAmount())) {
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        }*/
        //Assert断言处理异常
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean result = this.integralGradeService.save(integralGrade);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }

    }


}

